package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"movie/models"
	"strconv"
)

type MovieController struct {
	beego.Controller
}

func (c *MovieController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	query := models.DB.Model(&models.Movie{})
	title := c.GetString("title")
	if title != "" {
		query = query.Where("title like ?", "%"+title+"%")
	}
	key := "movie-" + strconv.Itoa(page) + "-" + title
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		var count int64
		err = query.Count(&count).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "总数查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
		offset := (page - 1) * size
		var movie []models.Movie
		err = query.Offset(offset).Limit(size).Find(&movie).Error
		pageData["count"] = count
		pageData["movie"] = movie
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonRes{
				Code: 200,
				Msg:  "mysql查询成功",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "redis查询成功",
			Data: pageData,
		}
		c.ServeJSON()
	}

}
func (c *MovieController) Add() {
	title := c.GetString("title")
	img := c.GetString("img")
	types := c.GetString("types")
	num, _ := c.GetInt("num")
	status := c.GetString("status")
	create := c.GetString("create")
	if title == "" || img == "" || types == "" {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "电影名称、电影封面、视频分类是必须上传的内容",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	movie := models.Movie{
		Title:  title,
		Img:    img,
		Types:  types,
		Num:    num,
		Status: status,
		Create: create,
	}
	err := models.DB.Create(&movie).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}

}
func (c *MovieController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "请上传文件",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgUrl := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "文件上传成功",
			Data: imgUrl,
		}
		c.ServeJSON()
	}
}
func (c *MovieController) Update() {
	title := c.GetString("title")
	img := c.GetString("img")
	types := c.GetString("types")
	num, _ := c.GetInt("num")
	status := c.GetString("status")
	create := c.GetString("create")
	id, _ := c.GetInt("id")
	movie := models.Movie{
		Title:  title,
		Img:    img,
		Types:  types,
		Num:    num,
		Status: status,
		Create: create,
	}
	err := models.DB.Where("ID=?", id).Updates(&movie).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *MovieController) Info() {
	id, _ := c.GetInt("id")
	key := "movie-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var movie models.Movie
	if err != nil {
		err = models.DB.Model(&models.Movie{}).Where("ID=?", id).First(&movie).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "详情展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&movie)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonRes{
				Code: 200,
				Msg:  "mysql详情展示成功",
				Data: movie,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &movie)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "redis详情展示成功",
			Data: movie,
		}
		c.ServeJSON()
	}
}
func (c *MovieController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.User{}, id).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		key := "movie-" + strconv.Itoa(id)
		models.Rdb.Del(key)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
